import 'package:enjoy_plus_ohos/utils/http.dart';
import 'package:enjoy_plus_ohos/utils/toast.dart';
import 'package:flutter/material.dart';

class NoticeDetail extends StatefulWidget {
  const NoticeDetail({super.key});

  @override
  State<NoticeDetail> createState() => _NoticeDetailState();
}

class _NoticeDetailState extends State<NoticeDetail> {
  // 公告详情数据
  Map noticeDetail = {};
  // 加载状态
  bool isLoading = true;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    // 获取路由参数
    final id = ModalRoute.of(context)?.settings.arguments;
    // 获取详情数据
    getNoticeDetail(id);
  }

  // 获取公告详情
  Future<void> getNoticeDetail(id) async {
    try {
      // 发送请求
      final res = await http.get('/announcement/$id');
      // 更新状态
      setState(() {
        noticeDetail = res['data'];
        isLoading = false;
      });
    } catch (e) {
      // 错误提示
      ToastUtil.showError('获取详情失败');
      // 返回上一页
      Navigator.pop(context);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('公告详情'),
        titleTextStyle: const TextStyle(
            color: Colors.black, fontSize: 18, fontWeight: FontWeight.bold),
        centerTitle: true,
        backgroundColor: Colors.white,
        elevation: 0, // 去除阴影
        iconTheme: const IconThemeData(color: Colors.black), // 返回按钮颜色
      ),
      body: isLoading
          ? const Center(child: CircularProgressIndicator())
          : SingleChildScrollView(
              padding: const EdgeInsets.all(15),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  // 标题
                  Text(
                    noticeDetail['title'] ?? '',
                    style: const TextStyle(
                      fontSize: 18,
                      fontWeight: FontWeight.bold,
                    ),
                  ),
                  // 时间和来源
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 15),
                    child: Row(
                      children: [
                        Text(
                          noticeDetail['createdAt'] ?? '',
                          style: const TextStyle(
                            color: Color(0xFF999999),
                            fontSize: 14,
                          ),
                        ),
                        const SizedBox(width: 15),
                        const Text(
                          '来源：享+社区',
                          style: TextStyle(
                            color: Color(0xFF999999),
                            fontSize: 14,
                          ),
                        ),
                      ],
                    ),
                  ),
                  // 内容
                  Text(
                    noticeDetail['content'] ?? '',
                    style: const TextStyle(
                      height: 1.8,
                      fontSize: 15,
                      color: Color(0xFF333333),
                    ),
                  ),
                ],
              ),
            ),
    );
  }
}
